#include "LuaVectorLibrary.h"
#include "Vec3.h"

extern "C" 
{
	#include "lua.h"
	#include "lualib.h"
	#include "lauxlib.h"
}

namespace Engine
{
	static int NewVec3(lua_State *L) 
	{
	    // First argument is now a table that represent the class to instantiate        
	    luaL_checktype(L, 1, LUA_TTABLE);   
	    
	    lua_newtable(L);      // Create table to represent instance
	
	    // Do function lookups in metatable
	    lua_pushvalue(L,1);
	    lua_setfield(L, 1, "__index");  
	
	    // Allocate memory for a pointer to to object
	    Vec3** v = (Vec3**)lua_newuserdata(L, sizeof(Vec3*));  
	
	    *v = new Vec3;
	    
	    luaL_getmetatable(L, "Engine.Vec3");

	    lua_setmetatable(L, -2);       
	    
	    // Set field '__self' of instance table to the user data
	    lua_setfield(L, -2, "__self");  
	    
	  return 1; 
	}

	static const luaL_Reg Vec3Funcs[] = 
	{
		// Creation
		{"New", NewVec3}, 
		{NULL, NULL}
	};
	
	void LuaVectorLibrary::Register(lua_State* l)
	{ /*
		lua(l, "Vec3", Vec3Funcs);
		lua_register(l, "Vec3", Vec3Funcs);  
		lua_pushvalue(l, -1);
		lua_setfield(l, -2, "__index"); 
		*/
    }
}